package org.example.judge_contest.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.judge_common.hander.RetrunMsgException;
import org.example.judge_common.util.IdWorker;
import org.example.judge_common.util.MyUtils;
import org.example.judge_contest.mapper.ContestProblemMapper;
import org.example.judge_contest.service.ContestProblemService;
import org.example.judge_contest.service.ContestRecordService;
import org.example.judge_pojo.ContestProblem;
import org.example.judge_pojo.Problem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.example.judge_feign_api.JudgeClient;

import java.io.Serializable;
import java.util.Date;
import java.util.List;


@Service("ContestProblemServiceImpl")
@Transactional
public class ContestProblemServiceImpl extends ServiceImpl<ContestProblemMapper, ContestProblem> implements ContestProblemService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private JudgeClient judgeClient;


    @Override
    public List<ContestProblem> listByContestId(String id) {
        QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
        contestProblemQueryWrapper.eq("contest_id",id).orderByAsc("sequence");


        List<ContestProblem> list = super.list(contestProblemQueryWrapper);

        return list;
    }

    @Override
    public boolean removeByContestId(Serializable contestId) {
        QueryWrapper<ContestProblem> wrapper = new QueryWrapper<>();
        wrapper.eq("contest_id",contestId);
        return super.remove(wrapper);
    }

    @Override
    public ContestProblem getByContestAndProblem(String contestId, String problemId) {
        QueryWrapper<ContestProblem> contestProblemQueryWrapper = new QueryWrapper<>();
        contestProblemQueryWrapper.eq("contest_id",contestId).eq("problem_id",problemId);
        return super.getOne(contestProblemQueryWrapper);
    }

    @Override
    public boolean save(List<ContestProblem> contestProblem) throws RetrunMsgException {
        //事先删除题目表
        if(contestProblem.size()>0)this.removeByContestId(contestProblem.get(0).getContestId());
        for (int i = 0; i < contestProblem.size(); i++) {
            contestProblem.get(i).setSequence(i+1);  //设置次序
            if(MyUtils.isEmpty(contestProblem.get(i).getContestId()))throw new RetrunMsgException("未指定比赛");
            if(MyUtils.isEmpty(contestProblem.get(i).getProblemId()))throw new RetrunMsgException("未指定题目");
            //判断是否重复
//            ContestProblem one = this.getByContestAndProblem(contestProblem.get(i).getContestId(),contestProblem.get(i).getProblemId());
//            if(one!=null){  //更新
//                this.updateById(contestProblem.get(i));
//            }else{  //删除
                this.save(contestProblem.get(i));
//            }


        }
        return true;
    }

    @Override
    public boolean save(ContestProblem entity) {
        if(MyUtils.isEmpty(entity.getId()))entity.setId(idWorker.nextId().toString());
        if(entity.getCreateTime()==null)entity.setCreateTime(new Date());
        if(MyUtils.isEmpty(entity.getContestTitle())){  //使用原名字
            //远程调用获取problem
            Object data = judgeClient.problemDetail(entity.getProblemId()).getData();
            String jsonString = JSON.toJSONString(data);
//            System.out.println(data);
            entity.setContestTitle(JSON.parseObject(jsonString,Problem.class).getTitle());
        }
        return super.save(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean updateById(ContestProblem entity) {
        if(MyUtils.isEmpty(entity.getContestTitle())){  //使用原名字
            //远程调用获取problem
            Object data = judgeClient.problemDetail(entity.getProblemId()).getData();
            String jsonString = JSON.toJSONString(data);
//            System.out.println(data);
            entity.setContestTitle(JSON.parseObject(jsonString,Problem.class).getTitle());
        }
        return super.updateById(entity);
    }
}
